//
//  DDInterviewTypeCell.m
//  DingDongZP
//
//  Created by 杨政涛 on 16/10/26.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

#import "DDInterviewTypeCell.h"

@implementation DDInterviewTypeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.interviewType = @[@"在线面试",@"到场面试"];
        self.videoInterviewTitle = @[@"面试时间",@"联系方式",@"面试地点",@"其他说明"];
        self.liveInterviewTitle = @[@"面试时间",@"其他说明"];
        
        self.windowShadowView = [[UIView alloc] init];
        self.windowShadowView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        self.windowShadowView.hidden = YES;
        self.windowShadowView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.3500];
        AppDelegate *app=(AppDelegate *)[UIApplication sharedApplication].delegate;
        [app.window.rootViewController.view addSubview:self.windowShadowView];
        
        UILabel *titleLabel = [UILabel labelWithFrame:CGRectMake(20,20, 80, 25) font:[UIFont systemFontOfSize:15] textColor:COLOR_FONT_BLACK text:@"面试类型"];
        [self.contentView addSubview:titleLabel];
        
        for (int j = 0; j< self.interviewType.count; j++) {
            
            UIButton *interviewTypeButton = [UIButton buttonWithType:UIButtonTypeCustom];
            interviewTypeButton.tag = 1+j;  // tag 值从1开始
            [interviewTypeButton setBackgroundImage:[UIImage imageNamed:@"d2"] forState:UIControlStateNormal];
            [interviewTypeButton setBackgroundImage:[UIImage imageNamed:@"d"] forState:UIControlStateSelected];
            interviewTypeButton.frame = CGRectMake(CGRectGetMaxX(titleLabel.frame)+(20+80)*j, 15, 80, 35);
            interviewTypeButton.titleLabel.font = [UIFont systemFontOfSize:14];
            [interviewTypeButton setTitle: self.interviewType[j] forState:UIControlStateNormal];
            [interviewTypeButton setTitleColor:COLOR_FONT_DARK_GRAY forState:UIControlStateNormal];
            [interviewTypeButton setTitleColor:COLOR_FONT_LIGHT_GREEN forState:UIControlStateSelected];
            if (j == 1) {
                
                interviewTypeButton.selected = YES;
                self.recodeButtonTag = 2;
            }
            [interviewTypeButton addTarget:self action:@selector(interviewTypeAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:interviewTypeButton];
        }
        
        UIImageView *dashedImageView = [[UIImageView alloc]init];
        dashedImageView = [self setupDivideLine_H:SCREEN_WIDTH];
        dashedImageView.frame = CGRectMake(10, 63, SCREEN_WIDTH-20, 1);
        [self.contentView addSubview:dashedImageView];
        
        [self creatLiveInterView];

    }
    return self;
}

/**
 *  选择面试类型
 */
- (void)interviewTypeAction:(UIButton *)sender
{
    
    if (self.recodeButtonTag == sender.tag){
        
        
    }else{
        
        for( UIView *view in [self.interviewBgView subviews] ){
            
            [view removeFromSuperview];
        }
        
        self.interviewTimeLabel.text = @"";
        self.fieldPhone.text = @"";
        self.fieldAddress.text = @"";
        self.fieldRemark.text = @"";
        
        UIButton *selectedButton = (UIButton *)[self viewWithTag:sender.tag];
        selectedButton.selected = YES;
        
        UIButton *recodeButton = (UIButton *)[self viewWithTag:self.recodeButtonTag];
        recodeButton.selected = NO;
        
        switch (sender.tag) {
                case 1:
                
                [self creatVideoInterView];
                break;
                
                case 2:
                
                [self creatLiveInterView];
                break;
            default:
                break;
        }
    }
    self.recodeButtonTag = sender.tag;
    
    
    if (self.didGetInterviewType) {
        
        self.didGetInterviewType(sender.tag);
    }
    
}


//到场面试
- (void)creatLiveInterView
{
    
    //到场面试的背景视图
    self.interviewBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 75, SCREEN_WIDTH, 320-75)];
    self.interviewBgView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.interviewBgView];
    
    CGFloat indexRemark; //其他说明 显示坐标的y点
    
    for (int i = 0; i<self.videoInterviewTitle.count; i++) {
        
        if (i == 3) {
            
            indexRemark = 25;
        }
        
        UILabel *titleLabel = [UILabel labelWithFrame:CGRectMake(20,indexRemark+(25+25)*i, 80, 25) font:[UIFont systemFontOfSize:15] textColor:COLOR_FONT_BLACK text:self.videoInterviewTitle[i]];
        [self.interviewBgView addSubview:titleLabel];
        
        if (i!=0) {
            
            UIImageView *dashedImageView = [[UIImageView alloc]init];
            dashedImageView = [self setupDivideLine_H:SCREEN_WIDTH];
            dashedImageView.frame = CGRectMake(10, CGRectGetMinY(titleLabel.frame)-12, SCREEN_WIDTH-20, 1);
            [self.interviewBgView addSubview:dashedImageView];
        }
        
        if (i == 0) {
            
            self.interviewTimeLabel = [UILabel labelWithFrame:CGRectMake(CGRectGetMaxX(titleLabel.frame), CGRectGetMinY(titleLabel.frame),SCREEN_WIDTH-CGRectGetMaxX(titleLabel.frame)-20, 25) font:[UIFont systemFontOfSize:14] textColor:COLOR_FONT_DARK_GRAY text:@""];
            self.interviewTimeLabel.userInteractionEnabled = YES;
            [self.interviewBgView addSubview:self.interviewTimeLabel];
            UITapGestureRecognizer *tapInterviewLabel = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooseTime)];
            [self.interviewTimeLabel addGestureRecognizer:tapInterviewLabel];
        }
        
        if (i == 1) {
            
            self.fieldPhone = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(titleLabel.frame), CGRectGetMinY(titleLabel.frame),SCREEN_WIDTH-CGRectGetMaxX(titleLabel.frame)-20, 25)];
            self.fieldPhone.tag = 100;  //tag = 100
            self.fieldPhone.delegate = self;
            self.fieldPhone.backgroundColor = [UIColor clearColor];
            self.fieldPhone.textColor = COLOR_FONT_DARK_GRAY;
            self.fieldPhone.font = [UIFont systemFontOfSize:14];
            self.fieldPhone.returnKeyType = UIReturnKeyDone;
            self.fieldPhone.clearButtonMode = UITextFieldViewModeWhileEditing;
            [self.interviewBgView addSubview:self.fieldPhone];
        }
        
        if (i == 2) {
            
            self.fieldAddress = [[UITextView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(titleLabel.frame), CGRectGetMinY(titleLabel.frame),SCREEN_WIDTH-CGRectGetMaxX(titleLabel.frame)-20, 50)];
            self.fieldAddress.tag = 100+i;   // 102
            self.fieldAddress.delegate = self;
            self.fieldAddress.backgroundColor = [UIColor clearColor];
            self.fieldAddress.textColor = COLOR_FONT_DARK_GRAY;
            self.fieldAddress.font = [UIFont systemFontOfSize:14];
            self.fieldAddress.returnKeyType = UIReturnKeyDone;
            [self.interviewBgView addSubview:self.fieldAddress];
        }
        
        if (i == 3) {
            
            self.fieldRemark = [[UITextView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(titleLabel.frame), CGRectGetMinY(titleLabel.frame),SCREEN_WIDTH-CGRectGetMaxX(titleLabel.frame)-20, 50)];
            self.fieldRemark.tag = 100+i;   // 103
            self.fieldRemark.delegate = self;
            self.fieldRemark.backgroundColor = [UIColor clearColor];
            self.fieldRemark.textColor = COLOR_FONT_DARK_GRAY;
            self.fieldRemark.font = [UIFont systemFontOfSize:14];
            self.fieldRemark.returnKeyType = UIReturnKeyDone;
            [self.interviewBgView addSubview:self.fieldRemark];
        }
    }
}

//在线面试
- (void)creatVideoInterView
{
    
    //在线面试的背景视图
    self.interviewBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 75, SCREEN_WIDTH, 320-75)];
    self.interviewBgView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.interviewBgView];
    
    for (int i = 0; i<self.liveInterviewTitle.count; i++) {
        
        
        UILabel *titleLabel = [UILabel labelWithFrame:CGRectMake(20,(25+25)*i, 80, 25) font:[UIFont systemFontOfSize:15] textColor:COLOR_FONT_BLACK text:self.liveInterviewTitle[i]];
        [self.interviewBgView addSubview:titleLabel];
        
        if (i!=0) {
            
            UIImageView *dashedImageView = [[UIImageView alloc]init];
            dashedImageView = [self setupDivideLine_H:SCREEN_WIDTH];
            dashedImageView.frame = CGRectMake(10, CGRectGetMinY(titleLabel.frame)-12, SCREEN_WIDTH-20, 1);
            [self.interviewBgView addSubview:dashedImageView];
        }
        
        if (i == 0) {
            
            self.interviewTimeLabel = [UILabel labelWithFrame:CGRectMake(CGRectGetMaxX(titleLabel.frame), CGRectGetMinY(titleLabel.frame),SCREEN_WIDTH-CGRectGetMaxX(titleLabel.frame)-20, 25) font:[UIFont systemFontOfSize:14] textColor:COLOR_FONT_DARK_GRAY text:@""];
            self.interviewTimeLabel.userInteractionEnabled = YES;
            [self.interviewBgView addSubview:self.interviewTimeLabel];
            UITapGestureRecognizer *tapInterviewLabel = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooseTime)];
            [self.interviewTimeLabel addGestureRecognizer:tapInterviewLabel];
            
        }
        
        if (i == 1) {
            
            self.fieldRemark = [[UITextView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(titleLabel.frame), CGRectGetMinY(titleLabel.frame),SCREEN_WIDTH-CGRectGetMaxX(titleLabel.frame)-20, 50)];
            self.fieldRemark.tag = 100+i; //tag = 101
            self.fieldRemark.delegate = self;
            self.fieldRemark.backgroundColor = [UIColor clearColor];
            self.fieldRemark.textColor = COLOR_FONT_DARK_GRAY;
            self.fieldRemark.font = [UIFont systemFontOfSize:14];
            self.fieldRemark.returnKeyType = UIReturnKeyDone;
            [self.interviewBgView addSubview:self.fieldRemark];
        }
        
    }
}

// 选择时间
- (void)chooseTime
{
    self.windowShadowView.hidden = NO;
    UIView *dateBgView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-300, SCREEN_WIDTH, 300)];
    dateBgView.backgroundColor = [UIColor whiteColor];
    [self.windowShadowView addSubview:dateBgView];
    //  时间选择器   UIDatePicker
    UIDatePicker *picker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0,50, SCREEN_WIDTH, CGRectGetHeight(dateBgView.bounds)-50)];
    picker.datePickerMode = UIDatePickerModeDateAndTime;
    //    设置最小的时间范围
    picker.minimumDate = [NSDate dateWithTimeIntervalSince1970:0];
    //    设置最大的时间范围
    picker.maximumDate = [NSDate dateWithTimeIntervalSinceNow:24*60*60*365*10];
    [picker addTarget:self action:@selector(didFinishSelectDate:) forControlEvents:UIControlEventValueChanged];
    [dateBgView addSubview:picker];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 50, 50);
    [button setImage:[UIImage imageNamed:@"common_back"] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithRed:0.165 green:0.612 blue:0.871 alpha:1.000] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(timePickViewButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [dateBgView addSubview:button];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(SCREEN_WIDTH-50, 0, 50, 50);
    [button2 setImage:[UIImage imageNamed:@"common_check"] forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor colorWithRed:0.165 green:0.612 blue:0.871 alpha:1.000] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(timePickViewButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [dateBgView addSubview:button2];
    
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 50)];
    titleLabel.centerX = dateBgView.centerX;
    titleLabel.text = @"面试时间";
    titleLabel.font = [UIFont systemFontOfSize:15];
    titleLabel.textColor = COLOR_FONT_DARK_GRAY;
    [dateBgView addSubview:titleLabel];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(button.frame), SCREEN_WIDTH, 0.5)];
    lineView.backgroundColor = COLOR_FONT_LIGHT_GRAY;
    [dateBgView addSubview:lineView];
    
}

- (void)didFinishSelectDate:(UIDatePicker *)sender
{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"YYYY-MM-dd HH:mm";
    NSString *time = [formatter stringFromDate:sender.date];
    self.interviewTimeLabel.text = time;
}

- (void)timePickViewButtonAction
{
    self.windowShadowView.hidden = YES;
}

#define UITextFieldDelegate

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}


-(void)textViewDidEndEditing:(UITextView *)textView
{


}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

/** 横着的虚线 */

- (UIImageView *)setupDivideLine_H:(CGFloat)lineWidth
{
    CGPoint beginPoint = CGPointZero;
    CGPoint endPoint = CGPointMake(lineWidth, 0);
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, lineWidth, 2)];
    UIGraphicsBeginImageContext(imageView.frame.size);   //开始画线
    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height) blendMode:kCGBlendModeHardLight alpha:1];
    
    CGContextRef lineContext = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(lineContext, UIColor.grayColor.CGColor);
    CGFloat lengths[] = {3, 3};
    CGContextSetLineDash(lineContext, 0, lengths, 2);  //画虚线
    CGContextMoveToPoint(lineContext, beginPoint.x, beginPoint.y);
    CGContextAddLineToPoint(lineContext, endPoint.x, endPoint.y);
    CGContextStrokePath(lineContext);
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    
    return imageView;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end

